package com.wenhui.nzapp.ui.fragment.main;

import static java.math.BigDecimal.*;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.gzwh.weblib.bean.Result;
import com.gzwh.weblib.utils.MyObserver;
import com.gzwh.weblib.utils.RequestUtils;
import com.wenhui.nzapp.AppContext;
import com.wenhui.nzapp.R;
import com.wenhui.nzapp.ui.activity.AboutActivity;
import com.wenhui.nzapp.ui.activity.LoginActivity;
import com.wenhui.nzapp.ui.activity.system.AccountActivity;
import com.wenhui.nzapp.ui.activity.system.PrintSetActivity;
import com.wenhui.nzapp.ui.activity.system.ShopSetActivity;
import com.wenhuitec.syslib.config.SysLoginConfig;

import java.math.BigDecimal;
import java.util.Map;

public class MyFragment extends Fragment {
    private Context mContext;

    private Activity mActivity;

    private TextView price_tv;

    private TextView count_tv;

    public MyFragment(Activity mActivity) {
        this.mActivity = mActivity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        mContext = view.getContext();
        price_tv = view.findViewById(R.id.price_tv);
        count_tv = view.findViewById(R.id.count_tv);
        view.findViewById(R.id.aboutBut).setOnClickListener(view1 -> {
            startActivity(new Intent(mContext, AboutActivity.class));
        });
        view.findViewById(R.id.accountBut).setOnClickListener(view1 -> {
            startActivity(new Intent(mContext, AccountActivity.class));
        });
        view.findViewById(R.id.shopSetBut).setOnClickListener(view1 -> {
            startActivity(new Intent(mContext, ShopSetActivity.class));
        });
        view.findViewById(R.id.printSetBut).setOnClickListener(view1 -> {
            startActivity(new Intent(mContext, PrintSetActivity.class));

        });
        view.findViewById(R.id.logoutBut).setOnClickListener(view1 -> {
            SysLoginConfig.getInstance(AppContext.getInstance()).setUserName("");
            SysLoginConfig.getInstance(AppContext.getInstance()).setPassword("");
            SysLoginConfig.getInstance(AppContext.getInstance()).setToken("");
            SysLoginConfig.getInstance(AppContext.getInstance()).setStatus(false);
            closeActivity();
        });
        return view;
    }

    public void closeActivity() {
        if (mActivity != null) {
            startActivity(new Intent(mContext, LoginActivity.class));
            mActivity.finish();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        RequestUtils.storePrice(mActivity, new MyObserver<Map<String, Object>>(mContext, false) {
            @Override
            public void onSuccess(Result<Map<String, Object>> result) {
                System.out.println(result.toString());
                try {
                    BigDecimal price = new BigDecimal(String.valueOf(result.getResult().get("price")))
                            .divide(new BigDecimal(100), ROUND_HALF_UP, 2).setScale(2);
                    price_tv.setText(price.toPlainString());

                    BigDecimal counts = new BigDecimal(String.valueOf(result.getResult().get("counts")));
                    count_tv.setText(counts.setScale(0).stripTrailingZeros().toPlainString());
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {

            }
        });
    }
}
